/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tbquestion_dal;

import dalInterface.DALFactory;
import dalfactory.DALFactoryImpl;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import sqlconnect.SQLConnectionFactory;
import utils.DefaultPort;

/**
 *
 * @author salm
 */
public class TBQuestion_DAL {

    private static String DALHost;
    private static int DALPort;
    
    private static void readHost()
    {
        // NEED TABLE
        try
        {
            Properties p = new Properties();
            p.load(new FileInputStream("NetworkConfig.ini"));
            
            DALHost = p.getProperty("DALHost");
            DALPort = Integer.parseInt(p.getProperty("DALPort"));
        }
        catch (IOException ioEx)
        {
            DALHost = "rmi://localhost";
            DALPort = 2013;
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws RemoteException {
        try
        {
            readHost();
            // TODO code application logic here
            Connection conn = SQLConnectionFactory.getInstance().createConnectionByConfigFile("ServerConfig.ini").getSqlConnection();
            System.out.println("DB connected");
            
            LocateRegistry.createRegistry(DefaultPort.getInstance().getDALPort());
            DALFactory dalFact = new DALFactoryImpl(conn);
            DALFactory exporetdDALFact = (DALFactory) UnicastRemoteObject.exportObject(dalFact, DefaultPort.getInstance().getDALPort());
            Naming.rebind(String.format("%s:%d/%s", DALHost, DALPort, "DALFactory"), exporetdDALFact);
            
            (new DALFrame()).setVisible(true);
        }
        catch (SQLException ex)
        {
            System.out.println("Can't connect to database");
            Logger.getLogger(TBQuestion_DAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (MalformedURLException ex) {
            Logger.getLogger(TBQuestion_DAL.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
